package com.dongqi.myblog.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dongqi.myblog.po.TBlog;
import com.dongqi.myblog.po.vo.BlogQuery;
import com.dongqi.myblog.po.vo.TagQuery;
import com.dongqi.myblog.po.vo.TypeQuery;
import com.dongqi.myblog.service.TBlogService;
import com.dongqi.myblog.service.TTagService;
import com.dongqi.myblog.service.TTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Api(description = "首页控制器")
@Controller
public class IndexController {

    @Autowired
    private TBlogService blogService;
    @Autowired
    private TTypeService tTypeService;
    @Autowired
    private TTagService tagService;

    /**
     * 网站封面
     */
    @GetMapping("/")
    public String cover(@RequestParam(defaultValue = "1") Long pageNo,
                        @RequestParam(defaultValue = "10") Long pageSize,
                        Model model){

        //1、封面推荐Blog（4个）
        QueryWrapper<TBlog> recWrapper = new QueryWrapper<>();
        recWrapper.eq("recommend",true)
                .eq("published",true)
                .orderByDesc("create_time");
        IPage<TBlog> pageRecommend = blogService.page(new Page<TBlog>(pageNo, 4), recWrapper);

        //2、最新文章（每页八条）
        Page<BlogQuery> pageLatest = blogService.listBlogs(new BlogQuery(), new Page<BlogQuery>(pageNo, 8));

        model.addAttribute("recommend",pageRecommend.getRecords());
        model.addAttribute("latest",pageLatest.getRecords());
        return "cover";
    }


    /**
     * 分页查询并跳转首页
     */
    @ApiOperation("跳转到首页的接口")
    @GetMapping("/index")
    public Object index(@RequestParam(defaultValue = "1") Long pageNo,
                        @RequestParam(defaultValue = "10") Long pageSize,
                        Model model){

        Page<BlogQuery> page = new Page<>(pageNo,pageSize);
        //index分页blog
        Page<BlogQuery> pageIndex = blogService.listIndex(null, page);
        QueryWrapper<TBlog> wrapper = new QueryWrapper();
        Page<TBlog> pageBlog = new Page<>(1,8);
        wrapper.eq("recommend",true)
               .eq("published",true)
               .orderByDesc("update_time");
        //推荐blog
        blogService.page(pageBlog,wrapper);
        //首页类型（降序）
        List<TypeQuery> typeindex = tTypeService.getTypeIndex(8);
        //首页标签（降序）
        List<TagQuery> tagIndex = tagService.getTagIndex(8);
        model.addAttribute("hasPrevious",pageIndex.hasPrevious());
        model.addAttribute("hasNext",pageIndex.hasNext());
        model.addAttribute("page",pageIndex);
        model.addAttribute("recommend",pageBlog.getRecords());
        model.addAttribute("types",typeindex);
        model.addAttribute("tags",tagIndex);
        return "index";
    }

    /**
     * 分页条件查询
     */
    @GetMapping("/search")
    public String search(@RequestParam(defaultValue = "1") Long pageNo,
                         @RequestParam(defaultValue = "10") Long pageSize,
                         String query,
                         Model model){

        Page<BlogQuery> page = new Page<>(pageNo,pageSize);
        Page<BlogQuery> pageQuery = blogService.listSearch(query,page);
        model.addAttribute("hasPrevious",pageQuery.hasPrevious());
        model.addAttribute("hasNext",pageQuery.hasNext());
        model.addAttribute("page",pageQuery);
        model.addAttribute("query",query);
        return "search";
    }

    /**
     * 跳转到关于我的页面
     */
    @RequestMapping("/about")
    public String about(){
        return "about";
    }

    @GetMapping("/footer/newblog")
    public String newblogs(Model model){
        QueryWrapper<TBlog> wrapper = new QueryWrapper();
        Page<TBlog> pageBlog = new Page<>(1,3);
        wrapper.eq("recommend",true)
                .eq("published",true)
                .orderByDesc("update_time");
        //推荐blog
        blogService.page(pageBlog,wrapper);
        model.addAttribute("newblogs",pageBlog.getRecords());
        //返回的是哪个页面下的哪个fragment
        return "_fragments :: newblogList";
    }

}
